/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.autocode.global.controller;

import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;

import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.core.context.PlatformContext;
import cn.com.ksplatform.modules.autocode.global.Config;
import cn.com.ksplatform.modules.autocode.global.adapter.IAdapter;
import cn.com.ksplatform.modules.autocode.global.adapter.IMethodAdapter;
import cn.com.ksplatform.modules.autocode.manager.model.Module;
import cn.com.ksplatform.modules.autocode.manager.model.Sence;
import cn.com.ksplatform.modules.autocode.util.XmlUtil;

/**
 * ClassName:ModulesController.java <br/>
 * Date: 2015年10月29日 <br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@Controller
@RequestMapping("/amodules/")
public class ModulesController {
	
	@Autowired
	VelocityConfigurer config;
	
	@RequestMapping("/{module}/{sence}")
	public void goModule(@PathVariable String module,@PathVariable String sence,HttpServletResponse response,HttpServletRequest request) throws Exception{
		Sence senceModel = getSence(module,sence);
		try {
			Class cls = Class.forName("cn.com.ksplatform.modules.autocode.global.adapter.Adapter_"+senceModel.getSenceType());
			IAdapter adapter =PlatformContext.getBean(cls);
			//ColumnArray columns = getColumns(moduleModle, senceModel);
			adapter.doAdapter(config,senceModel,request ,response);
		} catch (ClassNotFoundException e) {
			Log.error("没有找到对应的场景适配器");
			throw new Exception(e.getMessage());
		}
	}
	
	@RequestMapping("/{module}/{sence}/{method}")
	public void goMethod(@PathVariable String module,@PathVariable String sence,@PathVariable String method,HttpServletResponse response,HttpServletRequest request) throws Exception{
		Sence senceModel = getSence(module,sence);
		try {
			Class cls = Class.forName("cn.com.ksplatform.modules.autocode.global.adapter.Adapter_"+senceModel.getSenceType()+"_"+method);
			IMethodAdapter adapter = PlatformContext.getBean(cls);
			//ColumnArray columns = getColumns(moduleModle, senceModel);
			adapter.doAdapter(config,senceModel,request ,response);
		} catch (ClassNotFoundException e) {
			Log.error("没有找到对应的场景方法适配器");
			throw new Exception(e.getMessage());
		}
	}
	
	private Sence getSence(String moduleName,String senceName) throws Exception{
		File senceFile = new File(Config.PATH+File.separator+moduleName+File.separator+senceName+File.separator+"sence.xml");
		if(!senceFile.exists()){
			throw new Exception("场景不存在!!!");
		}
		Sence senceModel = (Sence) XmlUtil.getObjectFromXml(Sence.class, new FileInputStream(senceFile));
		
		File moduleFile = new File(Config.PATH+File.separator+moduleName+File.separator+"module.xml");
		if(!moduleFile.exists()){
			throw new Exception("模块不存在!!!");
		}
		Module module = (Module) XmlUtil.getObjectFromXml(Module.class, new FileInputStream(moduleFile));
		
		senceModel.setModule(module);
		return senceModel;
	}
	
}
